import java.util.ArrayList;
import java.util.Collections;

import java.util.Collections;
import java.util.Random;

// 这个类的对象表示一张牌

// 表示每个玩家的手牌
class HandCard {
    private ArrayList<Card> cards;
}

class Card {
    private String suit;
    private String rank;

    public Card(String suit, String rank) {
        this.suit = suit;
        this.rank = rank;
    }

    public String getSuit() {
        return suit;
    }

    public String getRank() {
        return rank;
    }

    public void  setSuit(String suit) {
        this.suit = suit;
    }

    public void setRank(String rank) {
        this.rank = rank;
    }

    public String toString() {
        return this.suit + this.rank;
    }
}

public class Teacher {
    public static void main(String[] args) {
        // 使用 ArrayList 表示一副扑克牌.
        ArrayList<Card> deck = createDeck();
        System.out.println("原始牌组: " + deck);
        // 洗牌
        // Collections.shuffle(deck);
        shuffle(deck);
        System.out.println("洗牌之后: " + deck);

        // 发牌. 假设有三个人, 每个人发 5 张牌.
        ArrayList<ArrayList<Card>> hands = new ArrayList<ArrayList<Card>>();
        // ArrayList<HandCard> handCards = new ArrayList<HandCard>();
        // 先创建 3 个人手里的牌的 ArrayList.
        for (int i = 0; i < 3; i++) {
            ArrayList<Card> hand = new ArrayList<Card>();
            hands.add(hand);
        }

        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                // 发牌是轮流的过程.
                ArrayList<Card> currentHand = hands.get(j);
                Card card = deck.remove(0);
                currentHand.add(card);
            }
        }

        // 打印每个人的手牌
        for (int i = 0; i < 3; i++) {
            System.out.println("第" + i + "个人的手牌: " + hands.get(i));
        }
    }

    // 自己实现以下洗牌
    public static void shuffle(ArrayList<Card> deck) {
        // 把整个 ArrayList 从后往前遍历.
        // 针对每次取到一张牌, 就生成一个随下标. 拿着当前位置的牌和随机下标位置的牌, 进行交换.
        Random random = new Random();
        for (int i = deck.size() - 1; i > 0; i--) {
            int j = random.nextInt(deck.size());
            // 交换两张牌的位置.
            Card tmp = new Card(deck.get(i).getSuit(), deck.get(i).getRank());
            deck.set(i, deck.get(j));
            deck.set(j, tmp);
        }
    }

    public static ArrayList<Card> createDeck() {
        // 创建一副扑克牌.
        ArrayList<Card> deck = new ArrayList<Card>();
        String[] suits = {"♠", "♥", "♦", "♣"};
        for (String suit : suits) {
            // 在每个花色中, 生成对应的点数的牌.
            for (int i = 2; i <= 10; i++) {
                Card card = new Card(suit, "" + i);
                deck.add(card);
            }
            Card cardJ = new Card(suit, "J");
            Card cardQ = new Card(suit, "Q");
            Card cardK = new Card(suit, "K");
            Card cardA = new Card(suit, "A");
            deck.add(cardJ);
            deck.add(cardQ);
            deck.add(cardK);
            deck.add(cardA);
        }
        return deck;
    }
}